package com.logistics.dictionary.controller;

import com.logistics.dictionary.entity.model.DictionaryPager;
import com.logistics.dictionary.entity.model.DictionarySave;
import com.logistics.dictionary.service.DictionaryService;
import com.logistics.utils.aspect.annotation.LoggerListener;
import com.logistics.utils.constants.LoggerMethod;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RequestMapping("sys/dictionary")
@Controller
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 查找列表
     * @param param
     * @return
     */
    @PostMapping("pager")
    @LoggerListener(businessName = "查询字典列表", methodName = LoggerMethod.SEARCH)
    public ResponseEntity pager(@RequestBody DictionaryPager param){
        return ResponseUtil.success(dictionaryService.pager(param));
    }

    /**
     * 根据类型查找字典数据
     * @param types
     * @return
     * @throws MessageException
     */
    @GetMapping("listByTypes/{types}")
    @LoggerListener(businessName = "根据类型查询字典列表", methodName = LoggerMethod.SEARCH)
    public ResponseEntity listByTypes(@PathVariable("types") String types) throws MessageException {
        return  ResponseUtil.success(dictionaryService.listByTypes(types));
    }

    /**
     * 查找详细
     * @param id
     * @return
     * @throws MessageException
     */
    @GetMapping("details/{id}")
    @LoggerListener(businessName = "查询字典详细", methodName = LoggerMethod.SEARCH)
    public ResponseEntity details(@PathVariable("id") String id) throws MessageException {
        return ResponseUtil.success(dictionaryService.details(id));
    }

    /**
     * 查找所有
     * @return
     */
    @GetMapping("allList")
    @LoggerListener(businessName = "获取字典所有数据", methodName = LoggerMethod.SEARCH)
    public ResponseEntity allList(){
        return ResponseUtil.success(dictionaryService.allList());
    }

    /**
     * 查找详细
     * @param ids
     * @return
     * @throws MessageException
     */
    @DeleteMapping("remove/{ids}")
    @LoggerListener(businessName = "删除字典信息", methodName = LoggerMethod.DELETE)
    public ResponseEntity remove(@PathVariable("ids") String ids) throws MessageException {
        dictionaryService.delete(ids);
        return ResponseUtil.success();
    }

    /**
     * 保存
     * @param save
     * @return
     */
    @PutMapping("save")
    @LoggerListener(businessName = "保存字典信息", methodName = LoggerMethod.SAVE)
    public ResponseEntity save(@RequestBody DictionarySave save){
        dictionaryService.save(save);
        return ResponseUtil.success();
    }
}
